/* $Id: ConfirmationDialog.java 37 2008-01-11 16:06:49Z qxodream $
 *
 * RssReaderMidlet --  RSS reader for J2ME(midp)
 * The original codes of this porgram is from
 * RssOwlMIDlet(zhengyun@gmail.com)
 *
 * Copyright (c) 2007-2008 QXO(qxodream@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package midp;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;

/**
 * 类说明：
 *
 * @author QXO(qxodream@gmail.com)
 *
 */
public class ConfirmationDialog extends Dialog implements CommandListener {

	public static final int YES = 0;
	public static final int NO = 1;
	protected Canvas canvas;
	protected Command noCommand;
	protected Command yesCommand;
	private String message;
	private String yesLabel;
	private String noLabel;

	private boolean showImage = false;

	public ConfirmationDialog(Display display, String message) {
		this(display, message, null, null);
	}

	public ConfirmationDialog(Display display, String amessage,
			String ayesLabel, String anoLabel) {
		super(display);
		this.message = (amessage == null) ? "继续操作？" : amessage;
		this.yesLabel = (yesLabel == null) ? "确定" : ayesLabel;
		this.noLabel = (noLabel == null) ? "返回" : anoLabel;

		yesCommand = new Command(yesLabel, Command.OK, 1);
		noCommand = new Command(noLabel, Command.CANCEL, 1);

	}

	protected void init() {
		canvas = showImage ? new WaitImageCanvas(message) : new DialogCanvas(
				message);
		canvas.addCommand(yesCommand);
		canvas.addCommand(noCommand);
		canvas.setCommandListener(this);
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void commandAction(Command c, Displayable d) {
		if (c == yesCommand) {
			doAction(YES);
		} else if (c == noCommand) {
			doAction(NO);
		}
	}

	protected Displayable getDisplayable() {
		return canvas;
	}

	public boolean isShowImage() {
		return showImage;
	}

	public void setShowImage(boolean showImage) {
		this.showImage = showImage;
	}

}
